package com.jkt.rms.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.acegisecurity.context.SecurityContextHolder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.jkt.rms.business.AppRole;
import com.jkt.rms.commons.utils.RMSException;

public class LoginController extends SimpleFormController {

	public Object formBackingObject(HttpServletRequest request)
			throws RMSException {
		AppRole backingObject = new AppRole();
		return backingObject;
	}

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws RMSException {
		Object obj = SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		// anonymousUser --> returned by Acegi Security if no one has logged in yet.
		if (obj.equals("anonymousUser"))
			return new ModelAndView("login");
		return new ModelAndView(new RedirectView("/loginsuccess.htm", true));
	}
}